package sorting;


public class RadixSort {
	public static String[] sort(String[] strs, int strLength)
	{
		String[] sorted = strs;
		for(int i = strLength - 1; i >= 0; i --)
		{
			sorted = sortOn(sorted, i);
		}
		
		return sorted;
	}
	
	public static String[] sortOn(String[] strs, int index)
	{
		char size = Character.MAX_VALUE;
		int[] count = new int[size];
		for(int i = 0, length = strs.length; i < length; i ++ )
		{
			count[strs[i].charAt(index)] ++;
		}
		
		for(int i = 1; i < size; i ++)
		{
			count[i] = count[i] + count[i - 1];
		}
		
		String[] sorted = new String[strs.length];
		
		for(int i = strs.length - 1; i >= 0; i --)
		{
			sorted[-- count[strs[i].charAt(index)]] = strs[i];
		}
		return sorted;
	}
	
	public static void main(String[] args) {

		String[] strs = {
				"12345",
				"13245",
				"54321",
				"25361",
				"hahah",
				"world",
				"morni",
		};
		sort(strs, 5);
	}
}
